# event for main menu
from EventDefine import event_MAINMENU,event_GAMEMGRFSM
from EventDefine import event_NETWORKGAME_LOGIN, event_NETWORKGAME_GAMELOBBY, \
                        event_NETWORKGAME_GAMESTART, event_NETWORKGAME_BACKTOMAINMENU
# gui
from Shell.gui.LoginFrame import LoginFrame
from Shell.gui.GameLobbyFrame import GameLobbyFrame

from direct.fsm import FSM

class GameLobbyFSM(FSM.FSM):
    def __init__(self):
        FSM.FSM.__init__(self, name = 'main menu')

        self.demand('Login')

    def enterLogin(self):
        self.acceptOnce(event_NETWORKGAME_GAMELOBBY, self.demand, ["GameLobby"]) # enter game lobby gui state
        self.acceptOnce(event_NETWORKGAME_BACKTOMAINMENU, self.demand, ["MainMenu"]) # leave game lobby
        self.LoginFrame = LoginFrame()

    def exitLogin(self):
        self.LoginFrame.destroy()
        self.ignoreAll()

    def enterGameLobby(self):
        self.acceptOnce(event_NETWORKGAME_GAMESTART, self.demand, ["GameStart"]) # enter game start
        self.acceptOnce(event_NETWORKGAME_BACKTOMAINMENU, self.demand, ["MainMenu"]) # leave game lobby
        self.GameLobbyFrame = GameLobbyFrame()

    def exitGameLobby(self):
        if self.newState == "MainMenu":
            bReleaseMap = True
        else:
            bReleaseMap = False
        self.GameLobbyFrame.destroy(bReleaseMap)
        self.ignoreAll()
        self.GameLobbyFrame = None  # delete instance of self.GameLobbyFrame

    def enterGameStart(self):
        self.ignoreAll()
        messenger.send(event_GAMEMGRFSM)

    def enterMainMenu(self):
        self.ignoreAll()
        messenger.send(event_MAINMENU)
